package com.control;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.controlbase.ControlBase;
import com.entity.Userinfo;
import com.service.UserService;

@Controller//表示此类是控制器
@RequestMapping("/demo01")
public class UserControl extends ControlBase{

	@Resource//自动注入
	private UserService userService;
	
	/**
	 * 登录
	 * @param name
	 * @param pwd
	 * @return
	 */
	@RequestMapping("/islogin.do")
	public String islogin(String name,String pwd)
	{
		Userinfo u=userService.islogin(name, pwd);
		if(u!=null)
		{
			request.getSession().setAttribute("u", u);
			request.getSession().setAttribute("name", name);
			return "main";
		}else
		{
			return "login";
		}
	}
	/**
	 * 添加用户
	 * @param user
	 * @return
	 */
	@RequestMapping("/add.do")
	public String add(Userinfo user)
	{
		int row=userService.add(user);
		if(row>0)
		{
			return "success";
		}else
		{
			return "error";
		}
	}
	
	/**
	 * 查询所有的用户
	 */
	@RequestMapping("/query.do")
	public String query()
	{
		List<Userinfo> list=userService.queryAll();
		request.getSession().setAttribute("list", list);
		return "query";
	}
	
	/**
	 * 根据id删除
	 */
	@RequestMapping("/delete.do")
	public String delete(int id)
	{
		int row=userService.delete(id);
		if(row>0)
		{
			return this.query();
		}else
		{
			return "error";
		}
	}
	
	/**
	 * 根据id查寻实体
	 */
	@RequestMapping("/queryById.do")
	public String queryById(int id)
	{
		Userinfo user=userService.queryById(id);
		request.getSession().setAttribute("user", user);
		return "update";
	}
	
	/**
	 * 根据id修改信息
	 */
	@RequestMapping("/update.do")
	public String update(int id,String name,String pwd)
	{
		int row=userService.update(id, name, pwd);
		if(row>0)
		{
			return this.query();
		}else
		{
			return "error";
		}
	}
	
	/**
	 * 查询个人信息
	 */
	@RequestMapping("/querymy.do")
	public String querymy(int id)
	{
		Userinfo user=userService.queryById(id);
		request.getSession().setAttribute("user", user);
		return "querymy";
	}
	/**
	 * 查询个人信息修改
	 */
	@RequestMapping("/gerenupdate.do")
	public String queryup(int id)
	{
		Userinfo user=userService.queryById(id);
		request.getSession().setAttribute("user", user);
		return "gerenupdate";
	}
	/**
	 * 根据id修改信息
	 */
	@RequestMapping("/updatemy.do")
	public String updatemy(int id,String name,String pwd)
	{
		int row=userService.update(id, name, pwd);
		if(row>0)
		{
			return "upsuccess";
		}else
		{
			return "error";
		}
	}
	
}
